package com.sxjh.controller.cb;


import cn.hutool.extra.cglib.CglibUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sxjh.common.BaseController;
import com.sxjh.config.Authorization;
import com.sxjh.common.Result;
import com.sxjh.entity.Materials;
import com.sxjh.entity.Settlement;
import com.sxjh.entity.param.*;
import com.sxjh.entity.result.*;
import com.sxjh.service.FinanceService;
import com.sxjh.service.MaterialsService;
import com.sxjh.service.SettlementService;
import com.sxjh.service.TruckingOrderDetailsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/coke")
@Api(tags = "焦炭")
public class Finance7Controller extends BaseController {

  @Autowired
  private MaterialsService materialsService;

  @Autowired
  private FinanceService financeService;

  @Autowired
  private TruckingOrderDetailsService truckingOrderDetailsService;

  @Autowired
  private SettlementService settlementService;

  @Authorization("cb.coke.list")
  @ApiOperation("派车单列表")
  @GetMapping
  Result<List<Finance7List2Result>> list2(@Validated Finance7ListParam param) {
    return ok(financeService.list26(param));
  }

  @ApiOperation("生成结算数据")
  @PostMapping("/create")
  @Authorization("cb.coke.create")
  Result<Void> create(@RequestBody @Validated Finance7CreateParam param) {
    try {
      financeService.settle(param);
    } catch (RuntimeException e) {
      logger.error(e.getMessage(), e);
      return err(e.getMessage());
    }
    return ok();
  }

  @ApiOperation("修改结算数据")
  @PutMapping("/update")
  @Authorization("cb.coke.update")
  Result<Void> update(@Validated @RequestBody Finance7UpdateParam param) {
    Settlement settlement = CglibUtil.copy(param, Settlement.class);
    return settlementService.updateById(settlement) ? ok() : err("修改失败");
  }

  @ApiOperation("焦炭结算列表")
  @GetMapping("/list")
  @Authorization("cb.coke.list3")
  Result<List<Finance7List3Result>> list3(@Validated Finance7List3Param param) {
    return ok(settlementService.list(param));
  }

  @ApiOperation("结算")
  @PostMapping("/settle")
  @Authorization("cb.coke.settle")
  Result<Finance7SettleAllResult> settle(@Validated @RequestBody Finance7SettleParam param) {
    if (param.getIds().size() <= 0) return err("ID集合不允许为空");
    return ok(settlementService.settle(param));
  }

  @ApiOperation("焦炭物料列表")
  @GetMapping("/materials")
  Result<List<MaterialListResult>> materials() {
    List<Materials> list = materialsService.list(
            new LambdaQueryWrapper<Materials>()
                    .select(Materials::getId, Materials::getName)
                    .eq(Materials::getCategoryId, 3)
                    .isNull(Materials::getDeletedAt));
    List<MaterialListResult> results = CglibUtil.copyList(list, MaterialListResult::new);
    return ok(results);
  }

//  @Authorization("cb.coke.list")
//  @GetMapping("/list")
//  @ApiOperation("列表")
  @Deprecated
  Result<List<Finance7ListResult>> list(@Validated Finance7ListParam param) {
    return ok(financeService.list20(param));
  }

//  @ApiOperation("回填数据")
//  @PostMapping("/fill")
//  @Authorization("cb.coke.fill")
  Result<Void> fill(@RequestBody @Validated Finance7FillParam param) {
    if (param.getBackfillId() != 0) return err("已填写过扣奖");
    try {
      financeService.backfill(param);
    } catch (Exception e) {
      logger.error(e.getMessage(), e);
      return err(e.getMessage());
    }
    return ok();
  }
}